package org.atlantis.grosbeak.depot.desc;

import java.util.ArrayList;
import java.util.List;

import org.atlantis.grosbeak.json.Jsons;

/**
 * 对象的描述。对象从六个方面来描述：
 * <ul>
 * <li>type：对象的类型，一个Class对象。</li>
 * <li>singleton：对象是否是单态，一个布尔值。</li>
 * <li>event：对象的监听，一个EventDesc对象。</li>
 * <li>args：对象的构造方法参数集合，一个ValueDesc集合。</li>
 * <li>field：对象的字段集合，一个FieldDesc集合。</li>
 * <li>scope：对象所保存的环境。</li>
 * </ul>
 * 
 * @author <a href="mailto:seenoevil.cn@gmail.com">SeeNoEvil</a>
 * 
 */
public class ObjectDesc implements Cloneable {

	private Class<?> type;

	private boolean singleton;

	private EventDesc event;

	private List<ValueDesc> args;

	private List<FieldDesc> fields;

	private String scope;

	public ObjectDesc() {
		args = new ArrayList<ValueDesc>();
		fields = new ArrayList<FieldDesc>();
		singleton = true;
	}

	public Class<?> getType() {
		return type;
	}

	public boolean isSingleton() {
		return singleton;
	}

	public EventDesc getEvent() {
		return event;
	}

	public String getScope() {
		return scope;
	}

	public void setType(Class<?> type) {
		this.type = type;
	}

	public void setSingleton(boolean singleton) {
		this.singleton = singleton;
	}

	public void setEvent(EventDesc event) {
		this.event = event;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public ValueDesc[] getArgs() {
		return args.toArray(new ValueDesc[args.size()]);
	}

	public boolean hasArgs() {
		return args.size() > 0;
	}

	public void addArg(ValueDesc arg) {
		args.add(arg);
	}

	public void addArgs(ValueDesc[] args) {
		this.args.clear();
		for (ValueDesc arg : args) {
			this.args.add(arg);
		}
	}

	public FieldDesc[] getFields() {
		return fields.toArray(new FieldDesc[fields.size()]);
	}

	public void addField(FieldDesc field) {
		this.fields.add(field);
	}

	public boolean hasField(String fieldName) {
		for (FieldDesc field : fields) {
			if (field.getName().equals(fieldName))
				return true;
		}
		return false;
	}

	public ObjectDesc clone() {
		return Jsons.fromJson(ObjectDesc.class, Jsons.toJson(this));
	}
}
